package com.web4code.codegen.gui;

import java.io.File;
import java.io.IOException;

import com.web4code.codegen.config.GenConfig;
import com.web4code.codegen.generator.GenerateProjectUtil;
import com.web4code.codegen.generator.GeneratorUtils;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.stage.DirectoryChooser;
import javafx.stage.Stage;

/**
 * 页面控制逻辑
 * 
 * @author web4code
 */
public class MainController {

    @FXML
    private ComboBox<String> typeComboBox;

    @FXML
    private Label datasourceParamLabel;

    @FXML
    private Label templatePathLabel;

    @FXML
    private Label generatePathLabel;

    /**
     * 界面展示内容初始化
     */
    public void initialize() {
        GenConfig genConfig = GenConfig.getGenConfig();
        // 为ComboBox组合框构造可选项
        typeComboBox.getItems().addAll("local", "MySQL");
        typeComboBox.setValue(genConfig.getDataSourceType());
        // 展示配置内容
        datasourceParamLabel.setText(genConfig.getDataSourceParam());
        templatePathLabel.setText(genConfig.getTemplatePath());
        generatePathLabel.setText(genConfig.getGeneratePath());
    }

    /**
     * 代码生成按钮点击
     * @param event 点击事件
     */
    @FXML
    public void handleBtnClick(ActionEvent event) {
        boolean isSuccess = false;
        try {
            // isSuccess = GenerateProjectUtil.generateProject();
            isSuccess = GeneratorUtils.generatorCodeAll();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //
        String buttonText = "success!";
        if (!isSuccess) {
            buttonText = "generate fail.";
        }
        Button btnSource = (Button) event.getSource();
        btnSource.setText(buttonText);
    }

    /**
     * “数据源参数”修改按钮点击
     * @param event 点击事件
     */
    @FXML
    public void clickChangeParam(ActionEvent event) {
        GenConfig genConfig = GenConfig.getGenConfig();
        //
        DirectoryChooser file = new DirectoryChooser();
        file.setTitle("请选择文件数据源路径");
        file.setInitialDirectory(new File(genConfig.getDataSourceParam()));
        Stage stage = (Stage) ((Node)event.getSource()).getScene().getWindow();
        File newFolder = file.showDialog(stage); // newFolder就是选择的文件夹
        //
        if (newFolder != null) {
            genConfig.setDataSourceParam(newFolder.getAbsolutePath());
            datasourceParamLabel.setText(genConfig.getDataSourceParam());
        }
    }

    /**
     * “模板路径”修改按钮点击
     * @param event 点击事件
     */
    @FXML
    public void clickChangeTemplate(ActionEvent event) {
        GenConfig genConfig = GenConfig.getGenConfig();
        //
        DirectoryChooser file = new DirectoryChooser();
        file.setTitle("请选择模板文件路径");
        file.setInitialDirectory(new File(genConfig.getTemplatePath()));
        Stage stage = (Stage) ((Node)event.getSource()).getScene().getWindow();
        File newFolder = file.showDialog(stage); // newFolder就是选择的文件夹
        //
        if (newFolder != null) {
            genConfig.setTemplatePath(newFolder.getAbsolutePath());
            templatePathLabel.setText(genConfig.getTemplatePath());
        }
    }

    /**
     * “代码路径”修改按钮点击
     * @param event 点击事件
     */
    @FXML
    public void clickChangeCodedir(ActionEvent event) {
        GenConfig genConfig = GenConfig.getGenConfig();
        //
        DirectoryChooser file = new DirectoryChooser();
        file.setTitle("请选择代码生成路径");
        file.setInitialDirectory(new File(genConfig.getGeneratePath()));
        Stage stage = (Stage) ((Node)event.getSource()).getScene().getWindow();
        File newFolder = file.showDialog(stage); // newFolder就是选择的文件夹
        //
        if (newFolder != null) {
            genConfig.setGeneratePath(newFolder.getAbsolutePath());
            generatePathLabel.setText(genConfig.getGeneratePath());
        }
    }
}
